using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Winter.BackEnd.Models.Domain;
using System.Security.Principal;
using System.Web.Security;

namespace Winter.BackEnd.Controllers
{
    [Authorize, HandleError]
    public class PersonController : ApplicationController
    {

        #region Actions

        public JsonResult GetDashbordInfo()
        {
            JsonResult result = new JsonResult();

            var events = Repository.CurrentUser.Person.Student.Group.GroupSchedules.First().Schedule.Events.Where(
                ev => ev.EventStartTime > DateTime.Now && ev.EventStartTime < DateTime.Now.AddDays(2));
            result.Data = new
            {
                msgCount = Repository.CurrentUser.Person.PersonMessages.Count(m => m.Message.IsNew && !m.Message.IsDeleted),
                events = (from ev in events
                          where ev.IsClass
                          select new
                          {
                              description = string.Format("{0}", ev.Subject.Name),
                              evtime = ev.EventStartTime.ToString(),
                              id = ev.ID
                          }).OrderBy(e => e.evtime)
            };
            return result;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            return View("Student", Repository.CurrentUser.Person.PersonInRole);
        }

        public ActionResult UserInfo(int? id)
        {
            return View("UserInfo", Repository.GetIPersonByPersonID(getPersonID(id).Value));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public string AddFriend(int userID)
        {

            Repository.CurrentDataContext.PersonFriends.InsertOnSubmit(new PersonFriend()
            {
                FriendID = userID,
                PersonID = Repository.CurrentUser.Person.ID
            });
            Repository.CurrentDataContext.SubmitChanges();

            return "Success";
        }

        public JsonResult GetFiendsInfo()
        {
            JsonResult result = new JsonResult();
            result.Data = new
            {
                friends = from personFriend in Repository.CurrentUser.Person.PersonFriends
                          select new
                          {
                              Name = personFriend.Friend.LastName + " " + personFriend.Friend.FirstName +
                                        " " + personFriend.Friend.SecondName,
                              Photo = personFriend.Friend.Image,
                              ID = personFriend.Friend.ID,
                              UserName = personFriend.Friend.UserName
                          }
            };
            return result;
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetPersonInfo(int? id)
        {
            var person = Repository.GetIPersonByPersonID(getPersonID(id).Value);
            var result = new
                                   {
                                       sex = person.Sex,
                                       birthdate = person.BirthDate,
                                       email = person.Email,
                                       duties = person.Duties
                                   };
            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Settings(int? id)
        {
            return View();
        }

        #endregion


    }
}
